/**
 * 
 */
package front_shop;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.JTableHeader;

/**
 * @author wx
 *
 */
public class neww extends JFrame implements ActionListener,ItemListener{
	JPanel panel;
	JLabel bh,lb;
	JComboBox<String> combox;
	JTextField spbh;
	JTable table;
	JButton delete,ok;
	Object t[][]= new Object[1][4];
	Connection con;
    Statement sql; 
    ResultSet rs;
	int i=0;
	String n="";
	String a="";
	
	/**
	 * @param args
	 */
	neww(){
			panel=new JPanel();
			lb=new JLabel("商品类别");
			bh=new JLabel("新商品编号");
			spbh=new JTextField(20);
			delete=new JButton("删除");
			ok=new JButton("提交");
			combox=new JComboBox<String>();
			combox.addItem("     ");
			combox.addItem("DQ电器");
			combox.addItem("LP礼品");
			combox.addItem("RY日用");
			combox.addItem("SC蔬菜");
			combox.addItem("SP食品");
			combox.addItem("SX生鲜");
			panel.add(combox);
			
			panel.setLayout(null);
			this.add(panel);
			panel.add(lb);
			lb.setBounds(20,20,80,20);
			panel.add(combox);
			combox.setBounds(80,20,80,20);
			panel.add(bh);
			bh.setBounds(200,20,80,20);
			panel.add(spbh);
			spbh.setBounds(280,20,80,20);
			

			panel.add(delete);
			delete.setBounds(120,100,80,20);
			panel.add(ok);
			ok.setBounds(300,100,80,20);
			Object columnname[]={"商品名称","单位","进价","售价"};
			table=new JTable(t,columnname);
			JTableHeader tableheader=table.getTableHeader();
			panel.add(tableheader);
			panel.add(table);
			tableheader.setBounds(20,50,460,20);
			table.setBounds(20,70,460,18);
			spbh.enable(false);
			ok.addActionListener(this);
			delete.addActionListener(this);
			combox.addItemListener(this);
			
	
				
			
			//combox.addItemListener(new );
			
			
			try{  
			    Class.forName("com.mysql.jdbc.Driver");
			    }
		 	catch(Exception e){
			       System.out.println("忘记把MySQL数据库的JDBC-数据库驱动程序复制到JDK的扩展目录中");
			    }
		 	try { String uri= "jdbc:mysql://127.0.0.1:3306/javashop";
			          String user="root";
			          String ps="123";
			          con=DriverManager.getConnection(uri,user,ps);
			          sql=con.createStatement();
			    }
		    catch(SQLException e){ 
			          System.out.println(e);
			    }
		
			
			
}
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub		
		
		String a=combox.getSelectedItem().toString().substring(0, 2);
		int  b = 0;
		try{
		rs=sql.executeQuery("SELECT * FROM merchandise ");//where MerchandiseNumber like \""+a+"%\"");
		while  (rs.next ()==true){
			b=Integer.parseInt(rs.getString(1))+1;//b 是编号
		}
		a=a+b;//新的商品编号
		n=a;
		spbh.setText(a);
		}
		catch(SQLException a1){
	          System.out.println(a1);
		}
	}
/*	public static void main(String[] args) {
		// TODO Auto-generated method stub
		neww user=new neww();
		user.setTitle("新增商品 ");
		user.setBounds(200,200,500,170);
		user.setVisible(true);
		user.validate();
		user.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}*/
	
	public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
		if(e.getSource()==ok){
			if (!t[0][0].equals("")&&!t[0][1].equals("")&&!t[0][2].equals("")&&!t[0][3].equals("")){
				if (Double.parseDouble(t[0][2].toString())<Double.parseDouble(t[0][3].toString())){
					String left2 = n.substring(0,2);
					String sortNumber = null;
					if(left2.equals("DQ") ){
						sortNumber = "1001";
					} else if(left2.equals("LP")){
						sortNumber = "1002";
					} else if(left2.equals("RY")){
						sortNumber = "1003";
					} else if(left2.equals("SC")){
						sortNumber = "1004";
					}  else if(left2.equals("SP")){
						sortNumber = "1005";
					} else if(left2.equals("SX")){
						sortNumber = "1006";
					} 
					a=n.substring(2);
					
					String w="insert into  merchandise values("+a+","+"\""+n+"\","+"\""+n+"\",\""+sortNumber+"\",\""+t[0][0].toString()+"\",\""+t[0][3].toString()+"\",\""+t[0][1].toString()+"\",\""+t[0][2].toString()+"\""+")";
					try{
						int ok=sql.executeUpdate(w);
						w="insert into  stock values("+a+","+"\""+n+"\","+0+")";
						ok=sql.executeUpdate(w);
						for(int i=0;i<4;i++){
							t[0][i]="";
						}
						spbh.setText("");
						table.repaint();
						 JOptionPane.showMessageDialog(this,"添加成功","新增商品",JOptionPane.WARNING_MESSAGE);
						 System.out.println(a);
						 replenish.s(a, n);
						n="";

						 this.dispose();
					}
					catch(SQLException c){
						System.out.println(c);
					}
				}
				else
				{
					 JOptionPane.showMessageDialog(this,"售价低于了进价！！！","新增商品",JOptionPane.WARNING_MESSAGE);
				}
			}
			else {
				 JOptionPane.showMessageDialog(this,"信息不能为空！！！","新增商品",JOptionPane.WARNING_MESSAGE);
			}
		}
		if(e.getSource()==delete){
			for(int i=0;i<4;i++){
				t[0][i]="";
			}
			spbh.setText("");
			n="";
		}
		
	}
	
}

